package com.example.itshome.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.itshome.R;
import com.example.itshome.activity.CircleParticularActivity;
import com.example.itshome.impl.ViewOutlineProviderImpl;
import com.example.itshome.entity.CircleEntity;
import com.example.itshome.network.AbstractNetworkRequest;
import com.example.itshome.network.GiveLikeRequest;

import java.util.List;

public class CircleListPageRecycleViewAdapter extends RecyclerView.Adapter<CircleListPageRecycleViewAdapter.CircleListPageViewHolder> {

    private final List<CircleEntity> mData;
    private final Context context;

    public CircleListPageRecycleViewAdapter(Context context,List<CircleEntity> list){
        mData=list;
        this.context=context;
    }


    @NonNull
    @Override
    public CircleListPageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view=View.inflate(context,R.layout.recycle_circle_list_page_item,null);
        view.setOutlineProvider(new ViewOutlineProviderImpl(20));
        view.setClipToOutline(true);
        CircleListPageViewHolder holder=new CircleListPageViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull CircleListPageViewHolder holder, int position) {
        CircleEntity entity=mData.get(position);
        holder.titleTextView.setText(entity.getTitle());
        holder.senderNameTextView.setText(entity.getSenderName());
        holder.sendTimeTextView.setText(entity.getSendTime());
        holder.likeCountTextView.setText(entity.getLikeCount());
        holder.likeImageView.setSelected(entity.isLiked());

        holder.likeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AbstractNetworkRequest request=new GiveLikeRequest((Activity) context)
                        .setLikeCountTextView(holder.likeCountTextView)
                        .setLikeImageView(holder.likeImageView)
                        .setEntity(entity);
                request.start();

            }
        });
        holder.wholeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(context, CircleParticularActivity.class);
                intent.putExtra("circleId",entity.getId());
                intent.putExtra("isLike",entity.isLiked());
                intent.putExtra("data",new String[]{
                        entity.getTitle(),
                        entity.getSenderName(),
                        entity.getSenderHeaderPictureSrc(),
                        entity.getSendTime(),
                        entity.getLikeCount()});
                context.startActivity(intent);
            }
        });

        Glide.with(context).load(entity.getPictureSrc()).placeholder(R.drawable.sample_picture).into(holder.imageView);

        RequestOptions options=new RequestOptions().circleCrop();
        Glide.with(context).load(entity.getSenderHeaderPictureSrc()).placeholder(R.drawable.sample_picture).apply(options).into(holder.senderHeaderImageView);
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    static class CircleListPageViewHolder extends RecyclerView.ViewHolder{

        ImageView imageView;
        ImageView senderHeaderImageView;
        ImageView likeImageView;
        TextView titleTextView;
        TextView senderNameTextView;
        TextView sendTimeTextView;
        TextView likeCountTextView;
        View likeLayout;
        View wholeLayout;
        public CircleListPageViewHolder(@NonNull View itemView) {
            super(itemView);
            likeImageView=itemView.findViewById(R.id.imv_recycle_circle_page_like);
            imageView=itemView.findViewById(R.id.imv_recycle_circle_page_image);
            senderHeaderImageView=itemView.findViewById(R.id.imv_recycle_circle_page_header_image);
            titleTextView=itemView.findViewById(R.id.txv_recycle_circle_list_title);
            senderNameTextView=itemView.findViewById(R.id.txv_recycle_circle_list_sender_name);
            sendTimeTextView=itemView.findViewById(R.id.txv_recycle_circle_list_time);
            likeCountTextView=itemView.findViewById(R.id.txv_recycle_circle_list_like_count);
            likeLayout=itemView.findViewById(R.id.layout_recycle_circle_list_like);
            wholeLayout=itemView;
        }
    }
}
